/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import android.content.Context;
import android.widget.SimpleAdapter;

import com.dpndeveloper.wtc.R;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.util.TimeUtil;

/**
 * Adapter to put WtcInterval in a ListView: It adapts the list of intervals
 * putting the work time day by day
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class DayListAdapter extends SimpleAdapter {

	private static final String	DAY	= "DAY";

	/**
	 * /** Constructor
	 * 
	 * @param context
	 *            The context where the View associated with this adapter is
	 *            running
	 * @param firstDay
	 *            the day to start to adapt the list
	 * @param intervals
	 *            the list of intervals to adapt
	 */
	public DayListAdapter(Context context, Calendar firstDay,
			List<WtcInterval> intervals) {
		super(context, getStrings(firstDay, intervals, context),
				R.layout.element_row, new String[] { DAY },
				new int[] { R.id.textViewRowElement });
	}

	/**
	 * 
	 * @param firstDay
	 *            the day to start to adapt the list
	 * @param intervals
	 *            the list of intervals to adapt
	 * @param context
	 *            the associated context
	 * @return the result list of elements
	 */
	private static List<Map<String, Object>> getStrings(Calendar firstDay,
			List<WtcInterval> intervals, Context context) {

		// Loop on the list refering to the end of a day
		Calendar dayEnd = (Calendar) firstDay.clone();
		dayEnd.set(Calendar.HOUR_OF_DAY, 23);
		dayEnd.set(Calendar.MINUTE, 59);
		dayEnd.set(Calendar.SECOND, 59);
		dayEnd.set(Calendar.MILLISECOND, 999);

		DateFormat format = new SimpleDateFormat("E dd/MM/yyyy");
		format.setTimeZone(TimeZone.getDefault());

		List<Map<String, Object>> dayList = new ArrayList<Map<String, Object>>();
		if (intervals != null) {

			Iterator<WtcInterval> iterator = intervals.iterator();

			// list of interval of a day
			List<WtcInterval> dayIntervals = new ArrayList<WtcInterval>();

			// Map to store the intervals day by day
			Map<Calendar, List<WtcInterval>> mapOfDay = new HashMap<Calendar, List<WtcInterval>>();

			while (iterator.hasNext()) {
				WtcInterval interval = iterator.next();

				// If an interval is not of the current day
				while (interval.getStartTime().getTimeInMillis() > dayEnd
						.getTimeInMillis()) {

					// move to next day and create a new related list
					dayIntervals = new ArrayList<WtcInterval>();
					dayEnd = (Calendar) dayEnd.clone();
					dayEnd.add(Calendar.DAY_OF_YEAR, 1);
				}

				// Add the interval to current day
				dayIntervals.add(interval);
				mapOfDay.put(dayEnd, dayIntervals);
			}

			// Sort the list by date
			List<Calendar> days = new ArrayList<Calendar>(mapOfDay.keySet());
			Collections.sort(days);

			// add the element to show looping into map
			for (Calendar day : days) {
				if (!mapOfDay.get(day).isEmpty()) {
					Calendar endDate = Calendar.getInstance(TimeZone.getDefault());
					if (day.compareTo(endDate)<0)
					{
						endDate = day;
					}
					String dayString = format.format(day.getTime())
							+ " - "
							+ TimeUtil.getWorkTimeAsString(mapOfDay.get(day),
									endDate);
					HashMap<String, Object> item = new HashMap<String, Object>();
					item.put(DAY, dayString);
					dayList.add(item);
				}
			}

		}
		return dayList;
	}
}
